In try and except and else block when the else block will be executed?
Question on basic python
Hello,
Thanks for reaching out on community! Looks like I will need a bit more context here. Can you expand on this one?
John
Hi @Mukesh,
Great question! I found this in the core Python docs on the try statement:
The optional else clause is executed if the control flow leaves the try suite, no exception was raised, and no return, continue, or break statement was executed. Exceptions in the else clause are not handled by the preceding except clauses.
In other words, the else
clause gets executed:
- Exception caught: no
- Exception not caught: no
- No exception raised at all: yes